import {createAction} from 'redux-actions';
import {
  GET_GOODS_LIST,
  GET_GOODS_INFO,
  GET_GOODS_KEYWORD,
  GET_GOODS_KEY,
  GET_GOODS_CATE,
  GET_GOODS_CONTENT,
  GET_GOODS_CATE_TOP,
  GET_GOODS_LIST_INDEX,
} from './../constants/actionTypes'
import * as goodsApi from './../apis/goods'


export const getGoodsListIndex = createAction(GET_GOODS_LIST_INDEX, async(params)=>{
  return await goodsApi.getGoodLists(params)
    .then((data) =>{
      if (data){
        return data
      }
      throw 'getGoodsListIndexError'
    })
}, (params)=>{
  return {
    ...params,
    sync: 'get Goods List Index'
  }
})


export const getGoodsList = createAction(GET_GOODS_LIST, async(params)=>{
  return await goodsApi.getGoodLists(params)
    .then((data) =>{
      if (data){
        return data
      }
      throw 'getGoodsListError'
    })
}, (params)=>{
  return {
    ...params,
    sync: 'get Goods List'
  }
})


export const getGoodsInfo = createAction(GET_GOODS_INFO, async(id)=>{
  return await goodsApi.getGoodInfo(id)
    .then((data) =>{
      if (data){
        return data
      }
      throw 'getGoodsListError'
    })
}, (id)=>{
  return {
    id,
    sync: 'get Goods List'
  }
})


export const getGoodsContent = createAction(GET_GOODS_CONTENT, async(id)=>{
  return await goodsApi.getGoodContent(id)
    .then((data) =>{
      if (data){
        return data
      }
      throw 'getGoodsContentError'
    })
}, (id)=>{
  return {
    id,
    sync: 'get Goods Content'
  }
})

export const getGoodsKeyword = createAction(GET_GOODS_KEYWORD, async(params)=>{
  return await goodsApi.getGoodKeyword(params)
    .then((data) =>{
      if (data){
        return data
      }
      throw 'getGoodsKeywordError'
    })
}, (id)=>{
  return {
    id,
    sync: 'get Goods Keyword'
  }
})

export const getGoodsKey = createAction(GET_GOODS_KEY, async(params)=>{
  return await goodsApi.getGoodKey(params)
    .then((data) =>{
      if (data){
        return data
      }
      throw 'getGoodsKeyError'
    })
}, (id)=>{
  return {
    id,
    sync: 'get Goods Key'
  }
})

export const getGoodsCate = createAction(GET_GOODS_CATE, async(params)=>{
    return await goodsApi.getGoodCat(params)
        .then((data) =>{
            if (data){
                return data
            }
            throw 'getGoodsCateError'
        })
}, (id)=>{
    return {
        id,
        sync: 'get Goods Cate'
    }
})

export const getGoodsCateTop = createAction(GET_GOODS_CATE_TOP, async()=>{
  return await goodsApi.getGoodCatTop()
    .then((data) =>{
      if (data){
        return data
      }
      throw 'getGoodsCateTopError'
    })
}, (id)=>{
  return {
    id,
    sync: 'get Goods Cate Top'
  }
})